const logger = require('../../../util/log');
const core = require('../core');
const config = require('../../config/config');

class Merchant {
    constructor(cookie) {
        this.cookie = cookie;
    }

    /**
     * 搜索关键字
     * @param keyword 关键字
     */
    async searchKeyword(keyword){
        let options = config.CRAWLER.MERCHANT_WORDS.CS_SEARCH_KEYWORD;
        //Build Search Url.
        let url = options.URL.render({searchUrl:keyword});
        options.cookie = this.cookie;
        logger.info(`Search Keyword. url:${url}`);
        let result = await core.executePageRequest(url,options);
        logger.info(`Parser MerchantWords Result:${JSON.stringify(result)}`);
        return result;
    }

    /**
     * 获取关键字详情
     * @param queryVo 查询参数
     */
    async keywordSearchDetail(queryVo){
        let options = config.CRAWLER.MERCHANT_WORDS.CS_DETAIL_KEYWORD;
        let url = options.URL;
        logger.info(`Search Keyword Detail. QueryVo:[${queryVo}] \n url:${url}`);
        options.body = JSON.parse(queryVo);
        options.cookie = this.cookie;
        let result = await core.executePageRequest(url,options);
        logger.info(`Parser MerchantWords Keyword Detail Result:${JSON.stringify(result)}`);
        return result;
    }

    /**
     * 获取ET列表
     */
    async etSearchList(path){
        let options = config.CRAWLER.MERCHANT_WORDS.ET_SEARCH;
        //Build Search Url.
        let url = options.URL.render({searchUrl:path});
        options.cookie = this.cookie;
        logger.info(`Search ET List. url:${url}`);
        let result = await core.executePageRequest(url,options);
        logger.info(`Parser MerchantWords ET List Result:${JSON.stringify(result)}`);
        return result;
    }

    /**
     * 获取p1搜索列表数据
     */
    async p1SearchList(path){
        let options = config.CRAWLER.MERCHANT_WORDS.P1_SEARCH;
        //Build Search Url.
        let url = options.URL.render({searchUrl:path});
        options.cookie = this.cookie;
        logger.info(`Search P1 List. url:${url}`);
        let result = await core.executePageRequest(url,options);
        logger.info(`Parser MerchantWords P1 List Result:${JSON.stringify(result)}`);
        return result;
    }

    /**
     * 搜索A+列表数据
     */
    async aplushSearchList(path){
        let options = config.CRAWLER.MERCHANT_WORDS.A_PLUSH_SEARCH;
        //Build Search Url.
        let url = options.URL.render({searchUrl:path});
        options.cookie = this.cookie;
        logger.info(`Search A+ List. url:${url}`);
        let result = await core.executePageRequest(url,options);
        logger.info(`Parser MerchantWords A+ List Result:${JSON.stringify(result)}`);
        return result;
    }

    /**
     * ET关键字搜索
     */
    async etSearchKeyword(path){
        let options = config.CRAWLER.MERCHANT_WORDS.ET_KEYWORD_SEARCH;
        //Build Search Url.
        let url = options.URL.render({searchUrl:path});
        options.cookie = this.cookie;
        logger.info(`Search ET Keyword List. url:${url}`);
        let result = await core.executePageRequest(url,options);
        logger.info(`Parser MerchantWords T Keyword List Result:${JSON.stringify(result)}`);
        return result;
    }
}

module.exports = Merchant;
